<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Linkslists;
use App\Models\Linksrecord;
use App\Models\Linkslistsstatistic;
use App\Models\Linksliststotal;
use App\Services\CommonService;
use App\Services\DeviceService;
use App\Services\OutputService;
use Illuminate\Support\Facades\Redis;
use Log,Exception;

class LinksstaticController extends Controller
{
    public function dailyStatistic(Request $request)
    {
        if (empty($request->links_id)) return OutputService::error("接口异常,请稍后尝试!");
        if (empty($request->date_start)) return OutputService::error("接口异常,请稍后尝试!");
        if (empty($request->date_end)) return OutputService::error("接口异常,请稍后尝试!");
        $linkslist = Linkslists::getLinksListsUsersId($request->links_id);
        if (empty($linkslist)) return OutputService::error("链接不存在或已删除!");
        if ($linkslist->users_id != $request->users_id) return OutputService::error("链接不存在或已删除!");

        $dateLists = CommonService::getDateFromRange($request->date_start,$request->date_end,"Ymd");
        $phases = CommonService::getDateFromRange($request->date_start,$request->date_end,"Ymd");
        $statistics = [];
        if ($request->diff_day < 1) {
            $startMonth = date("m",strtotime($request->date_start));
            // 按照小时查询
            $tbnum = ($startMonth%4);
            $linkRecordDb = new Linksrecord(["tbnum"=>$tbnum]);
            // 获取当天落地页访问
            $webTimeCounts = $linkRecordDb->getLinksRecordHour($request->links_id,"web_time",$request->date_start);
            // 获取当天小程序展示数量
            $miniShowTimes = $linkRecordDb->getLinksRecordHour($request->links_id,"mini_show_time",$request->date_start);
            // 获取当天长按识别数量
            $miniPressTimes = $linkRecordDb->getLinksRecordHour($request->links_id,"mini_press_time",$request->date_start);
            // 获取当天点击名片数量
            $miniCardTimes = $linkRecordDb->getLinksRecordHour($request->links_id,"mini_card_time",$request->date_start);
            // 获取当天直接跳转数量
            $miniRdirectTimes = $linkRecordDb->rdirectLinksRecordHour($request->links_id,$request->date_start);

            for ($i=0; $i < 25; $i++) {
                $hour = ($i < 10) ? "0".$i : $i;
                $page_view = collect($webTimeCounts)->where("hour",$hour)->sum("count");
                $mini_view = collect($miniShowTimes)->where("hour",$hour)->sum("count");
                $mini_press = collect($miniPressTimes)->where("hour",$hour)->sum("count");
                $mini_card = collect($miniCardTimes)->where("hour",$hour)->sum("count");
                $mini_direct = collect($miniRdirectTimes)->where("hour",$hour)->sum("count");
                $statistics[] = ["page_view"=>$page_view,"mini_view"=>$mini_view,"mini_direct"=>$mini_direct,"mini_press"=>$mini_press,"mini_card"=>$mini_card,"phase"=>$hour];
            }
        } else {
            // 按天查询
            $lastdatas = Linkslistsstatistic::getDailyLinksListsStatistics($request->links_id,$phases);
            foreach ($dateLists as $dateList) {
                $phase = str_replace("-", "", $dateList);
                $lastdata = $lastdatas->where("phase",$phase)->first();
                if (empty($lastdata)) {
                    $statistics[] = ["page_view"=>0,"mini_view"=>0,"mini_direct"=>0,"mini_press"=>0,"mini_card"=>0,"phase"=>$dateList];
                } else {
                    $statistics[] = ["page_view"=>$lastdata->page_view,"mini_view"=>$lastdata->mini_view,"mini_direct"=>$lastdata->mini_direct,"mini_press"=>$lastdata->mini_activate,"mini_card"=>$lastdata->mini_card,"phase"=>$dateList];
                }
            }
        }
        return OutputService::success("SUCCESS",["statistics"=>$statistics]);
    }

    public function otherStatistic(Request $request)
    {
        if (empty($request->links_id)) return OutputService::error("接口异常,请稍后尝试!");
        $linkslist = Linkslists::getLinksListsUsersId($request->links_id);
        if (empty($linkslist)) return OutputService::error("链接不存在或已删除!");
        if ($linkslist->users_id != $request->users_id) return OutputService::error("链接不存在或已删除!");
        // 获取数据库编号
        $startMonth = date("m",strtotime($request->other_date_start));
        $tbnum = ($startMonth%4);
        $linkRecordDb = new Linksrecord(["tbnum"=>$tbnum]);
        $start_time = strtotime($request->other_date_start);
        $end_time = strtotime($request->other_date_end);
        $links_id = $request->links_id;

        if ($request->other_chart == "system") {
            $systemdatas = $linkRecordDb->getSystemDatas($links_id);
            $systems = DeviceService::systemLists();
            $datas = [];
            foreach ($systemdatas as $systemdata) {
                if (empty($systemdata->system)) {
                    $system = "未知";
                } else {
                    $system = isset($systems[$systemdata->system]) ? $systems[$systemdata->system] : "未知";
                }
                $datas[] = ["value"=>$systemdata->system_count,"name"=>empty($systemdata->system) ? "未知" :$system];
            }
            return OutputService::success("SUCCESS",["datas"=>$datas]);
        } elseif ($request->other_chart == "browser") {
            $browsers = DeviceService::browserNamesLists();
            $systemdatas = $linkRecordDb->getBrowserDatas($links_id);
            $datas = [];
            foreach ($systemdatas as $systemdata) {
                if (empty($systemdata->browser)) {
                    $browser = "未知";
                } else {
                    $browser = isset($browsers[$systemdata->browser]) ? $browsers[$systemdata->browser] : "未知";
                }
                $datas[] = ["value"=>$systemdata->browser_count,"name"=>$browser];
            }
            return OutputService::success("SUCCESS",["datas"=>$datas]);
        } elseif ($request->other_chart == "device") {
            $devices = DeviceService::deviceNames();
            $systemdatas = $linkRecordDb->getDeviceDatas($links_id);
            $datas = [];
            foreach ($systemdatas as $systemdata) {
                if (empty($systemdata->device)) {
                    $device = "未知";
                } else {
                    $device = isset($devices[$systemdata->device]) ? $devices[$systemdata->device] : "未知";
                }
                $datas[] = ["value"=>$systemdata->device_count,"name"=>$device];
            }
            return OutputService::success("SUCCESS",["datas"=>$datas]);
        } elseif ($request->other_chart == "rate") {
            if ($request->diff_day <= 1) {
                $page_view = $linkRecordDb->getDailyCount($links_id,"web_time",$start_time,$end_time);
                $mini_view = $linkRecordDb->getDailyCount($links_id,"mini_show_time",$start_time,$end_time);
                $mini_direct = $linkRecordDb->getDailyDirectCount($links_id,$start_time,$end_time);
                $mini_press = $linkRecordDb->getDailyCount($links_id,"mini_press_time",$start_time,$end_time);
                $mini_card = $linkRecordDb->getDailyCount($links_id,"mini_card_time",$start_time,$end_time);
            } else {
                $phases = CommonService::getDateFromRange($request->other_date_start,$request->other_date_end,"Ymd");
                $linkStatic = Linkslistsstatistic::getDailyLinksListsStatistics($links_id,$phases);
                $page_view = $linkStatic->sum("page_view");
                $mini_view = $linkStatic->sum("mini_view");
                $mini_direct = $linkStatic->sum("mini_direct");
                $mini_press = $linkStatic->sum("mini_press");
                $mini_card = $linkStatic->sum("mini_card");
            }
            $total = $page_view+$mini_direct;
            if ($total == 0) {
                $total_rate = 0;
                $mini_view_rate = 0;
                $mini_press_rate = 0;
                $mini_card_rate = 0;
            } else {
                $total_rate = 100;
                $mini_view_rate = round($mini_view/$total,2)*100;
                $mini_press_rate = round($mini_press/$total,2)*100;
                $mini_card_rate = round($mini_card/$total,2)*100;
            }
            $datas = [["value"=>$total_rate,"name"=>"访问落地页"],["value"=>$mini_view_rate,"name"=>"二维码展示量"],["value"=>$mini_press_rate,"name"=>"长按识别量"],["value"=>$mini_card_rate,"name"=>"点击名片量"]];
            return OutputService::success("SUCCESS",["datas"=>$datas]);
        } elseif ($request->other_chart == "map") {
            $systemdatas = $linkRecordDb->getProvinceDatas($request->links_id);
            $datas = [];
            foreach ($systemdatas as $systemdata) {
                $datas[] = ["value"=>$systemdata->province_count,"name"=>empty($systemdata->province) ? "未知" : $systemdata->province];
            }
            return OutputService::success("SUCCESS",["datas"=>$datas]);
        } else {
            return OutputService::error("参数错误!");
        }
    }

    public function totalStatistic(Request $request)
    {
        if (empty($request->links_id)) return OutputService::error("接口异常,请稍后尝试!");
        $linkslist = Linkslists::getLinksListsUsersId($request->links_id);
        if (empty($linkslist)) return OutputService::error("链接不存在或已删除!");
        if ($linkslist->users_id != $request->users_id) return OutputService::error("链接不存在或已删除!");
        $today = date("Ymd",time());
        $page_view = Redis::hget('page_view_'.$today, $request->links_id);
        $page_view = $page_view ? $page_view : 0;
        $mini_view = Redis::hget('mini_view_'.$today, $request->links_id);
        $mini_view = $mini_view ? $mini_view : 0;
        $mini_press = Redis::hget('mini_activate_'.$today, $request->links_id);
        $mini_press = $mini_press ? $mini_press : 0;
        $mini_card = Redis::hget('mini_card_'.$today, $request->links_id);
        $mini_card = $mini_card ? $mini_card : 0;
        $mini_direct = Redis::hget('mini_direct_'.$today, $request->links_id);
        $mini_direct = $mini_direct ? $mini_direct : 0;

        $totalStatic = Linksliststotal::getLinkListTotal($request->links_id);
        if (empty($totalStatic)) {
             Linksliststotal::createLinkListTotal($request->links_id);
             $totalStatic = (object)["page_view"=>0,"mini_direct"=>0,"mini_view"=>0,"mini_press"=>0,"mini_card"=>0];
        }

        return OutputService::success("SUCCESS",["totalStatic"=>["page_view"=>$totalStatic->page_view,"mini_direct"=>$totalStatic->mini_direct,"mini_view"=>$totalStatic->mini_view,"mini_press"=>$totalStatic->mini_press,"mini_card"=>$totalStatic->mini_card],"todayStatic"=>["page_view"=>$page_view,"mini_view"=>$mini_view,"mini_press"=>$mini_press,"mini_card"=>$mini_card,"mini_direct"=>$mini_direct]]);
    }
}
